package com.paziy.card_games.decks;

import com.paziy.card_games.cards.*;
import java.util.*;


/**
 * Created with IntelliJ IDEA.
 * User: paziy
 * Date: 12.02.13
 * Time: 12:05
 * To change this template use File | Settings | File Templates.
 */
public class HiddenDeck implements Deck {

    private static Card  card = CardsFactory.getCard(Suit.SIDE,Nominal.SIDE);

    private Iterator<Card> iterator = new Iterator<Card>(){

        private int position = 0;


        public boolean hasNext() {
            if (Long.valueOf(position) + Long.valueOf(1) <= Long.valueOf(size)) return true;
            return false;
        }


        public Card next() throws IllegalStateException{
            if (position == size) throw new IllegalStateException();

            ++position;

            return CardsFactory.getCard(Suit.SIDE,Nominal.SIDE);

        }

        public void remove() {
            if (size > 0) HiddenDeck.this.remove(1);
        }

    };

    private int size;

    HiddenDeck() {
        size = 0;
    }

    HiddenDeck(int size) {
        this.size = size;
    }

    HiddenDeck(Collection<Card> cards) {
        this(cards.size());
    }

    @Override
    public int size() {
        return size;
    }

    @Override
    public boolean isEmpty() {
        return size == 0 ? true : false;
    }


    @Override
    public Iterator<Card> iterator() {
        return iterator;
    }

    @Override
    public void add(Card card) {
        //todo exceptions
        if (size < Integer.MAX_VALUE) {
            ++size;
        }

    }

    @Override
    public boolean offer(Card card) {
        if (size < Integer.MAX_VALUE) {
            ++size;
            return true;
        }
        return false;
    }

    @Override
    public Card poll() {
        if (size > 0) {
            --size;
            //todo card type?
            return card;
        }
        return null;
    }

    @Override
    public Card element() {
        return iterator.next();
    }

    @Override
    public Card peek() {
        if(!iterator.hasNext()) return null;
        return iterator.next();
    }

    @Override
    public Card remove() throws IllegalStateException {
        if (remove(1)){
            //todo what card?
            return card;
        }

        throw new IllegalStateException();
    }

    public boolean remove(int count) {
        if (size >= count) {
            size -=count;
            return true;
        }

        return false;
    }


    @Override
    public boolean addAll(Collection<? extends Card> c) {
        if (Long.valueOf(size) + Long.valueOf(c.size()) <= Long.valueOf(Integer.MAX_VALUE)) {
            size += c.size();
            return true;
        }

        return false;
    }

    @Override
    public boolean removeAll(Collection<?> c) {
        return false;  //todo write method
    }

    @Override
    public void clear() {
        size = 0;
    }


    public void shuffle() {}

    @Override
    public String toString() {
        return card.toString() + String.valueOf(size);
    }

    public static void main(String[] args) {
        Deck d = new HiddenDeck(5);


        d.offer(CardsFactory.getCard(Suit.SIDE,Nominal.SIDE));

        for (Card c : d) {
            System.out.println(c);
        }
    }
}
